<!--
 * @Description: 培训课程表
 * @Autor: dyx
 * @Date: 2023-05-30 14:36:16
 * @LastEditTime: 2023-06-01 17:26:08
-->
<template>
  <div class="TISchedule">
    <div style="padding-right: 7px;">
      <el-button type="primary" style="float:right" @click="exportWord"
        >导出Word</el-button
      >
    </div>
    <div class="mytable" v-loading="loading">
      <div class="table_title">培训课程表</div>
      <el-row class="gaugeOutfit">
        <el-col :span="12" style="padding-left:10px">
          培训班名称：
          <input type="text" v-model="dataForm.className" style="width:75%" />
        </el-col>

        <el-col :span="12" style="padding-left:10px"
          >培训机构：
          <input type="text" v-model="dataForm.pxjg" style="width:75%" />
        </el-col>
      </el-row>

      <table>
        <tr>
          <th width="50%">
            课程名称
          </th>
          <th width="10%">
            课时
          </th>
          <th>
            授课教师
          </th>
        </tr>
        <tr v-for="(item, index) in list" :key="index">
          <td>
            <input
              type="text"
              v-model="item.coursewareName"
              style="width:100%"
            />
          </td>
          <td>
            <input
              type="text"
              v-model="item.coursewareTime"
              style="width:100%;text-align:center;"
              oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"
            />
          </td>
          <td>
            <input
              type="text"
              style="width:100%;text-align:center;"
              v-model="item.teacherName"
            />
          </td>
        </tr>

        <tr>
          <td rowspan="2">
            合计课时数
          </td>
          <td colspan="2" rowspan="2">
            {{ total }}
          </td>
        </tr>
        <tr>
          <!-- <td colspan="2" style="text-align: center;" valign="middle">博客地址：https://blog.csdn.net/pan_junbiao</td> -->
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
export default {
  name: "TISchedule",
  data() {
    return {
      loading: true,
      dataForm: {},
      list: []
    };
  },
  created() {},
  computed: {
    total() {
      let total = 0;
      this.list.forEach(item => {
        total += Number(item.coursewareTime);
      });
      this.dataForm.total = total;
      return total;
    }
  },
  methods: {
    init(id) {
      this.loading = true;

      request({
        url: "/api/class_archives/generateA2/" + id+`?organizeId=${this.$store.getters.organizeId}`,
        method: "get"
      }).then(res => {
        this.dataForm = res.data;
        // 创建10位数组
        this.list = Array.apply(null, { length: 10 }).map(() => {
          return { coursewareName: "", coursewareTime: "", teacherName: "" };
        });
        // 获取数据数组长度
        let lgh = this.dataForm.childrenVOList.length;
        // 替换数组
        this.list.splice(0, lgh, ...this.dataForm.childrenVOList);
        this.loading = false;
      });
    },
    exportWord() {
      this.dataForm.childrenVOList = this.list;
      for (let key in this.dataForm) {
        if (Array.isArray(this.dataForm[key])) {
          this.dataForm[key].forEach(item => {
            for (let key2 in item) {
              if (item[key2] == null) {
                item[key2] = "";
              }
            }
          });
        } else if (this.dataForm[key] == null) {
          this.dataForm[key] = "";
        }
      }
      this.dataForm.organizeId=this.$store.getters.organizeId;
      request({
        responseType: "blob",
        url: "/api/class_archives/generateA2Export",
        method: "post",
        data: this.dataForm
      }).then(res => {
        var debug = res;
        if (debug) {
          var a = document.createElement("a");
          a.download = "培训课程表.docx";
          a.style.display = "none";
          var blob = new Blob([debug], { type: "application/x-msdownload" });
          a.href = URL.createObjectURL(blob);
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        }
      });
    }
  }
};
</script>
<style scoped lang="scss">
// 引入公共样式
@import "./TABLESTYLE.scss";
</style>
